require 'test/unit'
require 'watir'

         #~ $un="crux@eforceglobal.com"
         #~ $pwd="wafer"
         #~ $ip="10.7.10.178"
         #~ #$ie = Watir::IE.start('http://'+$ip+'/cruxWebUI/CruxHome.aspx')
         #~ $filepath="E:/Crux_Automation"
          
    class TC_05SubLocations < Test::Unit::TestCase  
 
  #~ def test_01Login
  
         #~ require 'watir'

        #~ puts "User is going to login to the Crux Application"
        #~ sleep 8
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtUserName$txtWaterMarkedTextBox").set $un
        #~ $ie.text_field(:name,"ctl00$ContentPlaceHolder1$txtPassword$txtWaterMarkedTextBox").set $pwd
        #~ $ie.button(:name,"ctl00$ContentPlaceHolder1$btnLogin").click
        #~ puts "Successfully logged into the Application"
        #~ sleep 10

    #~ end
    
    
def test_02AddSubLocation
 
require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Add_SubLocation.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Add SubLocation Functionality"
puts "**************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
   puts "Verifying existing Company : '#{record['CompanyName']}' for Location"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkLocations").click    
    sleep 8
  
    puts "Verifying Location : '#{record['LName']}'for creating SubLocation '#{record['SLName']}'"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][2].text 
    #puts u    
    if u=="#{record['LName']}"      
      puts "Location :'#{record['LName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][5].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_sublocations.gif").click  
      sleep 5
      $ie.button(:name,"ctl00$ContentPlaceHolder1$btnAddNewSubLocation").click
      sleep 5
      puts "Creating Sub Location: '#{record['SLName']}'"
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtSubLocationName_txtWaterMarkedTextBox').set(record['SLName'])
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtSubLocationAddress_txtWaterMarkedTextBox').set(record['SLAddress'])
      $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtSubLocationComments_txtWaterMarkedTextBox').set(record['SLComments']) 
    
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 5   
      puts "SubLocation: '#{record['SLName']}' created successfully"
 
      puts "Verifying the SubLocation: '#{record['SLName']}'"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations").row_count
   
     #puts c
     
     for k in 2..rc do
      eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations")[k][2].text 
    #puts eu  
    if eu=="#{record['SLName']}"      
      puts "SubLocation :'#{record['SLName']}' verified successfully"
      break
    end
    end      
      break
    end
    end
    break
  end 
end
end   
 
end
  

def test_03EditSubLocation
 require 'xls'
require 'watir'

xlFile = XLS.new($filepath + "/Crux_Data/Edit_SubLocation.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "**************************"
puts "Edit SubLocation Functionality"
puts "**************************"

myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
   puts "Verifying existing Company : '#{record['CompanyName']}' for Location"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkLocations").click    
    sleep 8
  
    puts "Verifying Location : '#{record['LName']}'for updating SubLocation '#{record['SLName']}'"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][2].text 
    #puts u    
    if u=="#{record['LName']}"      
      puts "Location :'#{record['LName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][5].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_sublocations.gif").click  
      sleep 8
      #-----------------------------------------------------------      
      #-----------------------------------------------------------------------
 
      puts "Verifying the SubLocation: '#{record['SLName']}'for update"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations").row_count
   
     #puts c
     
     for k in 2..rc do
      eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations")[k][2].text 
    #puts eu  
    if eu=="#{record['SLName']}"      
      puts "SubLocation :'#{record['SLName']}' verified successfully"
      puts"Updating the SubLocation: '#{record['SLName']}'as '#{record['ESLName']}'"
       $ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations")[k][5].button(:src,"http://"+$ip+"/CruxWebUI/appimages/edit_sublocations.gif").click  
      sleep 8
       $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtSubLocationName_txtWaterMarkedTextBox').set(record['ESLName'])
     $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtSubLocationAddress_txtWaterMarkedTextBox').set(record['ESLAddress'])
    $ie.text_field(:id, 'ctl00_ContentPlaceHolder1_txtSubLocationComments_txtWaterMarkedTextBox').set(record['ESLComments']) 
    
    $ie.button(:id,"ctl00_ContentPlaceHolder1_btnSave").click
    sleep 5
    puts"SubLocation: '#{record['SLName']}'successfully updated as '#{record['ESLName']}'"
    puts "Verifying the updated SubLocation: '#{record['ESLName']}'"
    rc1=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations").row_count
    for m in 2..rc1 do
      sl=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations")[m][2].text 
    #puts eu  
    if sl=="#{record['ESLName']}"      
      puts "Updated SubLocation :'#{record['ESLName']}' verified successfully"
      break
    end
    end      
      break
    end
    end      
      break
    end
    end
    break
  end 
end
end   
 
 end
 
 
 def test_04DeleteSubLocation

require 'xls'
require 'watir'
require 'watir/ie' 
require 'watir/contrib/enabled_popup'

xlFile = XLS.new($filepath + "/Crux_Data/Delete_SubLocation.xls") #grab the data file in the same dirrectory
myData = xlFile.getRowRecords('','Sheet1')  #pull data records  from excel
xlFile.close

puts "********************************"
puts "Delete SubLocation Functionality"
puts "********************************"


myData.each do |record|
  
  $ie.link(:text,"Administration").click
  sleep 5

  $ie.link(:text,"Manage Companies").click
   sleep 5
  
   puts "Verifying a Company : '#{record['CompanyName']}' for SubLocation"   
 
   b=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies").row_count
   
     #puts b
     
     for i in 2..b do
   a=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][2].text 
    #puts a
    if a=="#{record['CompanyName']}"      
     puts "Company: '#{record['CompanyName']}' verified successfully"   
    sleep 5
     
    $ie.table(:id,"ctl00_ContentPlaceHolder1_grdCompanies")[i][7].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_company.gif").click
    sleep 5
    $ie.link(:id,"ctl00_ContentPlaceHolder1_lnkLocations").click    
    sleep 8
  
    puts "Verifying Location : '#{record['LName']}'for deleting SubLocation '#{record['SLName']}'"
    c=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations").row_count
     for j in 2..c do
   u=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][2].text 
    #puts u    
    if u=="#{record['LName']}"      
      puts "Location :'#{record['LName']}' verified successfully"
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdLocations")[j][5].button(:src,"http://"+$ip+"/CruxWebUI/appimages/manage_sublocations.gif").click  
      sleep 8     
 
      puts "Verifying the SubLocation: '#{record['SLName']}'for delete"
 
      rc=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations").row_count
   
     #puts c
     
     for k in 2..rc do
      eu=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations")[k][2].text 
    #puts eu  
    if eu=="#{record['SLName']}"      
      puts "SubLocation :'#{record['SLName']}' verified successfully"
      puts"Deleting the SubLocation: '#{record['SLName']}'"
      
      $ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations")[k][5].button(:src,"http://"+$ip+"/CruxWebUI/appImages/delete.gif").click_no_wait
      sleep 5
      
      #--------------------------------------------------------------------------------------------
      
       hwnd = $ie.enabled_popup(5)

       if (hwnd)  #yeah! a popup
       popup = WinClicker.new
       popup.makeWindowActive(hwnd)
       popup.clickWindowsButton("Microsoft Internet Explorer", "OK", 30)
       end
 
       sleep 8      

      #-----------------------------------------------------------------------------------------------
  

    puts"SubLocation: '#{record['SLName']}'deleted successfully"
    puts "Verifying the deleted SubLocation: '#{record['SLName']}'"
    
     rc1=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations").row_count
    
    for m in 2..rc1 do
      sl=$ie.table(:id,"ctl00_ContentPlaceHolder1_grdSubLocations")[m][2].text 
    #puts eu  
    if sl=="#{record['SLName']}"
flag=1      
      break
    end
  end 
if flag==1
puts "SubLocation '#{record['SLName']}' not deleted successfully"
else
  puts "Deleted SubLocation '#{record['SLName']}' verified successfully"
  end
      break
    end
    end      
      break
    end
    end
    break
  end 
end
end   
 
end


  #~ def test_17Logout
    
            #~ puts "Logging out from the Application"
            #~ $ie.link(:id,"ctl00_Logout_lnkLogout").click
            #~ puts "Successfully logged out from the Application"
            #~ puts "********************************"
            #~ puts "End of SubLocations Functionality"
            #~ puts "********************************"
            #~ sleep 5
            #~ $ie.close
            
          #~ end

 

 end 



